local OVALE, Ovale = ...
local OvaleScripts = Ovale.OvaleScripts

do
	local name = "simulationcraft_paladin_protection_t17m"
	local desc = "[6.1] AttiMod: Paladin_Protection_T17M"
	local code = [[
# Based on SimulationCraft profile "Paladin_Protection_T17M".
#	class=paladin
#	spec=protection
#	talents=3032322
#	glyphs=focused_shield/divine_protection/final_wrath

#ArdentDefender
AddListItem(ArdentDefender ad0 "ArdentDefender  0")
AddListItem(ArdentDefender ad1 "ArdentDefender  10")
AddListItem(ArdentDefender ad2 "ArdentDefender  20")
AddListItem(ArdentDefender ad3 "ArdentDefender  30")
AddListItem(ArdentDefender ad4 "ArdentDefender  40")
AddListItem(ArdentDefender ad5 "ArdentDefender  50" default)
AddListItem(ArdentDefender ad6 "ArdentDefender  60")
AddListItem(ArdentDefender ad7 "ArdentDefender  70")
AddListItem(ArdentDefender ad8 "ArdentDefender  80")
AddListItem(ArdentDefender ad9 "ArdentDefender  90")
AddListItem(ArdentDefender ad10 "ArdentDefender  100")

AddFunction ArdentDefenderValue asValue=1
{
	if List(ArdentDefender ad0) 0
	if List(ArdentDefender ad1) 10
	if List(ArdentDefender ad2) 20
	if List(ArdentDefender ad3) 30
	if List(ArdentDefender ad4) 40
	if List(ArdentDefender ad5) 50
	if List(ArdentDefender ad6) 60
	if List(ArdentDefender ad7) 70    
	if List(ArdentDefender ad8) 80 
	if List(ArdentDefender ad9) 90 
	if List(ArdentDefender ad10) 101 
}

#LayOnHands
AddListItem(LayOnHands lol0 "LayOnHands  0")
AddListItem(LayOnHands lol1 "LayOnHands  10")
AddListItem(LayOnHands lol2 "LayOnHands  20")
AddListItem(LayOnHands lol3 "LayOnHands  30")
AddListItem(LayOnHands lol4 "LayOnHands  40")
AddListItem(LayOnHands lol5 "LayOnHands  50" default)
AddListItem(LayOnHands lol6 "LayOnHands  60")
AddListItem(LayOnHands lol7 "LayOnHands  70")
AddListItem(LayOnHands lol8 "LayOnHands  80")
AddListItem(LayOnHands lol9 "LayOnHands  90")
AddListItem(LayOnHands lol10 "LayOnHands  100")

AddFunction LayOnHandsValue asValue=1
{
	if List(LayOnHands lol0) 0
	if List(LayOnHands lol1) 10
	if List(LayOnHands lol2) 20
	if List(LayOnHands lol3) 30
	if List(LayOnHands lol4) 40
	if List(LayOnHands lol5) 50
	if List(LayOnHands lol6) 60
	if List(LayOnHands lol7) 70    
	if List(LayOnHands lol8) 80 
	if List(LayOnHands lol9) 90 
	if List(LayOnHands lol10) 101 
}

#Guardian
AddListItem(Guardian gd0 "Guardian  0")
AddListItem(Guardian gd1 "Guardian  10")
AddListItem(Guardian gd2 "Guardian  20")
AddListItem(Guardian gd3 "Guardian  30")
AddListItem(Guardian gd4 "Guardian  40")
AddListItem(Guardian gd5 "Guardian  50" default)
AddListItem(Guardian gd6 "Guardian  60")
AddListItem(Guardian gd7 "Guardian  70")
AddListItem(Guardian gd8 "Guardian  80")
AddListItem(Guardian gd9 "Guardian  90")
AddListItem(Guardian gd10 "Guardian  100")

AddFunction GuardianValue asValue=1
{
	if List(Guardian gd0) 0
	if List(Guardian gd1) 10
	if List(Guardian gd2) 20
	if List(Guardian gd3) 30
	if List(Guardian gd4) 40
	if List(Guardian gd5) 50
	if List(Guardian gd6) 60
	if List(Guardian gd7) 70    
	if List(Guardian gd8) 80 
	if List(Guardian gd9) 90 
	if List(Guardian gd10) 101 
}

#WoG
AddListItem(WoG wog0 "WoG  0")
AddListItem(WoG wog1 "WoG  10")
AddListItem(WoG wog2 "WoG  20")
AddListItem(WoG wog3 "WoG  30")
AddListItem(WoG wog4 "WoG  40")
AddListItem(WoG wog5 "WoG  50" default)
AddListItem(WoG wog6 "WoG  60")
AddListItem(WoG wog7 "WoG  70")
AddListItem(WoG wog8 "WoG  80")
AddListItem(WoG wog9 "WoG  90")
AddListItem(WoG wog10 "WoG  100")

AddFunction WoGValue asValue=1
{
	if List(WoG wog0) 0
	if List(WoG wog1) 10
	if List(WoG wog2) 20
	if List(WoG wog3) 30
	if List(WoG wog4) 40
	if List(WoG wog5) 50
	if List(WoG wog6) 60
	if List(WoG wog7) 70    
	if List(WoG wog8) 80 
	if List(WoG wog9) 90 
	if List(WoG wog10) 101 
}

#DivineProtection
AddListItem(DivineProtection dp0 "DivineProtection  0")
AddListItem(DivineProtection dp1 "DivineProtection  10")
AddListItem(DivineProtection dp2 "DivineProtection  20")
AddListItem(DivineProtection dp3 "DivineProtection  30")
AddListItem(DivineProtection dp4 "DivineProtection  40")
AddListItem(DivineProtection dp5 "DivineProtection  50" default)
AddListItem(DivineProtection dp6 "DivineProtection  60")
AddListItem(DivineProtection dp7 "DivineProtection  70")
AddListItem(DivineProtection dp8 "DivineProtection  80")
AddListItem(DivineProtection dp9 "DivineProtection  90")
AddListItem(DivineProtection dp10 "DivineProtection  100")

AddFunction DivineProtectionValue asValue=1
{
	if List(DivineProtection dp0) 0
	if List(DivineProtection dp1) 10
	if List(DivineProtection dp2) 20
	if List(DivineProtection dp3) 30
	if List(DivineProtection dp4) 40
	if List(DivineProtection dp5) 50
	if List(DivineProtection dp6) 60
	if List(DivineProtection dp7) 70    
	if List(DivineProtection dp8) 80 
	if List(DivineProtection dp9) 90 
	if List(DivineProtection dp10) 101 
}



Include(ovale_common)
Include(ovale_trinkets_mop)
Include(ovale_trinkets_wod)
Include(ovale_paladin_spells)

AddCheckBox(opt_interrupt L(interrupt) default specialization=protection)
AddCheckBox(opt_melee_range L(not_in_melee_range) specialization=protection)
AddCheckBox(opt_potion_armor ItemName(draenic_armor_potion) default specialization=protection)
AddCheckBox(opt_righteous_fury_check SpellName(righteous_fury) default specialization=protection)
AddCheckBox(opt_auto_blessing auto_blessing default specialization=protection)

AddListItem(opt_using_apl normal L(normal_apl) default)
AddListItem(opt_using_apl max_dps "max_dps APL")
AddListItem(opt_using_apl max_survival "max_survival APL")

AddFunction ProtectionUsePotionArmor
{
	if CheckBoxOn(opt_potion_armor) and target.Classification(worldboss) Item(draenic_armor_potion usable=1)
}

AddFunction ProtectionGetInMeleeRange
{
	#if CheckBoxOn(opt_melee_range) and not target.InRange(rebuke) Texture(misc_arrowlup help=L(not_in_melee_range))
	if CheckBoxOn(opt_melee_range) and not target.InRange(rebuke)
	{
		if target and target.HealthPercent() <= 20 Spell(hammer_of_wrath)
		Spell(consecration)
		Spell(judgment)
		Spell(avengers_shield)
		Spell(holy_prism)
		Spell(holy_wrath)
	}
}

AddFunction ProtectionInterruptActions
{
	if CheckBoxOn(opt_interrupt) and not target.IsFriend() and target.IsInterruptible()
	{
		Spell(avengers_shield)
		if target.InRange(rebuke) Spell(rebuke)
		if not target.Classification(worldboss)
		{
			if target.InRange(fist_of_justice) Spell(fist_of_justice)
			if target.InRange(hammer_of_justice) Spell(hammer_of_justice)
			Spell(blinding_light)
			Spell(arcane_torrent_holy)
			if target.InRange(quaking_palm) Spell(quaking_palm)
			Spell(war_stomp)
		}
	}
}

AddFunction ProtectionTimeToHPG
{
	if Talent(sanctified_wrath_talent) SpellCooldown(crusader_strike holy_wrath judgment)
	if not Talent(sanctified_wrath_talent) SpellCooldown(crusader_strike judgment)
}

### actions.default

AddFunction ProtectionDefaultMainActions
{
	#seal_of_insight,if=talent.empowered_seals.enabled&!seal.insight&buff.uthers_insight.remains<cooldown.judgment.remains
	if Talent(empowered_seals_talent) and not Stance(paladin_seal_of_insight) and BuffRemaining(uthers_insight_buff) < SpellCooldown(judgment) Spell(seal_of_insight)
	#seal_of_righteousness,if=talent.empowered_seals.enabled&!seal.righteousness&buff.uthers_insight.remains>cooldown.judgment.remains&buff.liadrins_righteousness.down
	if Talent(empowered_seals_talent) and not Stance(paladin_seal_of_righteousness) and BuffRemaining(uthers_insight_buff) > SpellCooldown(judgment) and BuffExpires(liadrins_righteousness_buff) Spell(seal_of_righteousness)
	#avengers_shield,if=buff.grand_crusader.react&active_enemies>1&!glyph.focused_shield.enabled
	if BuffPresent(grand_crusader_buff) and Enemies() > 1 and not Glyph(glyph_of_focused_shield) Spell(avengers_shield)
	#hammer_of_the_righteous,if=active_enemies>=3
	if Enemies() >= 2 Spell(hammer_of_the_righteous)
	if Enemies() >= 2 Spell(consecration)
	#crusader_strike
	Spell(crusader_strike)
	#wait,sec=cooldown.crusader_strike.remains,if=cooldown.crusader_strike.remains>0&cooldown.crusader_strike.remains<=0.35
	unless SpellCooldown(crusader_strike) > 0 and SpellCooldown(crusader_strike) <= 0.35 and SpellCooldown(crusader_strike) > 0
	{
		#judgment,cycle_targets=1,if=glyph.double_jeopardy.enabled&last_judgment_target!=target
		if Glyph(glyph_of_double_jeopardy) and BuffPresent(glyph_of_double_jeopardy_buff) Spell(judgment text=double)
		#judgment
		Spell(judgment)
		#wait,sec=cooldown.judgment.remains,if=cooldown.judgment.remains>0&cooldown.judgment.remains<=0.35
		unless SpellCooldown(judgment) > 0 and SpellCooldown(judgment) <= 0.35 and SpellCooldown(judgment) > 0
		{
			#avengers_shield,if=active_enemies>1&!glyph.focused_shield.enabled
			if Enemies() > 1 and not Glyph(glyph_of_focused_shield) Spell(avengers_shield)
			#holy_wrath,if=talent.sanctified_wrath.enabled
			if Talent(sanctified_wrath_talent) Spell(holy_wrath)
			#avengers_shield,if=buff.grand_crusader.react
			if BuffPresent(grand_crusader_buff) Spell(avengers_shield)
			#sacred_shield,if=target.dot.sacred_shield.remains<2
			if BuffRemaining(sacred_shield_buff) < 2 Spell(sacred_shield)
			#holy_wrath,if=glyph.final_wrath.enabled&target.health.pct<=20
			if Glyph(glyph_of_final_wrath) and target.HealthPercent() <= 20 Spell(holy_wrath)
			#avengers_shield
			Spell(avengers_shield)
			#holy_prism,if=!talent.seraphim.enabled|buff.seraphim.up|cooldown.seraphim.remains>5|time<5
			if not Talent(seraphim_talent) or BuffPresent(seraphim_buff) or SpellCooldown(seraphim) > 5 or TimeInCombat() < 5 Spell(holy_prism)
			#hammer_of_wrath
			Spell(hammer_of_wrath)
			#sacred_shield,if=target.dot.sacred_shield.remains<8
			if BuffRemaining(sacred_shield_buff) < 8 Spell(sacred_shield)
			#holy_wrath
			Spell(holy_wrath)
			#seal_of_insight,if=talent.empowered_seals.enabled&!seal.insight&buff.uthers_insight.remains<=buff.liadrins_righteousness.remains
			if Talent(empowered_seals_talent) and not Stance(paladin_seal_of_insight) and BuffRemaining(uthers_insight_buff) <= BuffRemaining(liadrins_righteousness_buff) Spell(seal_of_insight)
			#seal_of_righteousness,if=talent.empowered_seals.enabled&!seal.righteousness&buff.liadrins_righteousness.remains<=buff.uthers_insight.remains
			if Talent(empowered_seals_talent) and not Stance(paladin_seal_of_righteousness) and BuffRemaining(liadrins_righteousness_buff) <= BuffRemaining(uthers_insight_buff) Spell(seal_of_righteousness)
			#sacred_shield
			#Spell(sacred_shield)
			#flash_of_light,if=talent.selfless_healer.enabled&buff.selfless_healer.stack>=3
			if Talent(selfless_healer_talent) and BuffStacks(selfless_healer_buff) >= 3 Spell(flash_of_light)
		}
	}
}

AddFunction ProtectionDefaultShortCdActions
{
	#auto_attack
	#ProtectionGetInMeleeRange()
	#speed_of_light,if=movement.remains>1
	if 0 > 1 Spell(speed_of_light)
	#seraphim
	Spell(seraphim)
	#eternal_flame,if=buff.eternal_flame.remains<2&buff.bastion_of_glory.react>2&(holy_power>=3|buff.divine_purpose.react|buff.bastion_of_power.react)
	if BuffRemaining(eternal_flame_buff) < 2 and {HolyPower() >= 3 or BuffPresent(divine_purpose_buff) } and HealthPercent() <= WoGValue() { Spell(eternal_flame) Spell(word_of_glory) }
	#eternal_flame,if=buff.bastion_of_power.react&buff.bastion_of_glory.react>=5
	#if BuffPresent(bastion_of_power_buff) and BuffStacks(bastion_of_glory_buff) >= 1 and HealthPercent() <= WoGValue() { Spell(eternal_flame) Spell(word_of_glory) }
	#shield_of_the_righteous,if=buff.divine_purpose.react
	if target.InRange(rebuke)
	{
		if BuffPresent(divine_purpose_buff) Spell(shield_of_the_righteous)
		#shield_of_the_righteous,if=(holy_power>=5|incoming_damage_1500ms>=health.max*0.3)&(!talent.seraphim.enabled|cooldown.seraphim.remains>5)
		if { HolyPower() >= 5 or IncomingDamage(1.5) >= MaxHealth() * 0.3 } and { not Talent(seraphim_talent) or SpellCooldown(seraphim) > 5 } Spell(shield_of_the_righteous)
		#shield_of_the_righteous,if=buff.holy_avenger.remains>time_to_hpg&(!talent.seraphim.enabled|cooldown.seraphim.remains>time_to_hpg)
		if BuffRemaining(holy_avenger_buff) > ProtectionTimeToHPG() and { not Talent(seraphim_talent) or SpellCooldown(seraphim) > ProtectionTimeToHPG() } Spell(shield_of_the_righteous)
    }
	unless Talent(empowered_seals_talent) and not Stance(paladin_seal_of_insight) and BuffRemaining(uthers_insight_buff) < SpellCooldown(judgment) and Spell(seal_of_insight) or Talent(empowered_seals_talent) and not Stance(paladin_seal_of_righteousness) and BuffRemaining(uthers_insight_buff) > SpellCooldown(judgment) and BuffExpires(liadrins_righteousness_buff) and Spell(seal_of_righteousness) or BuffPresent(grand_crusader_buff) and Enemies() > 1 and not Glyph(glyph_of_focused_shield) and Spell(avengers_shield) or Enemies() >= 2 and Spell(hammer_of_the_righteous) or Spell(crusader_strike)
	{
		#wait,sec=cooldown.crusader_strike.remains,if=cooldown.crusader_strike.remains>0&cooldown.crusader_strike.remains<=0.35
		unless SpellCooldown(crusader_strike) > 0 and SpellCooldown(crusader_strike) <= 0.35 and SpellCooldown(crusader_strike) > 0
		{
			unless Glyph(glyph_of_double_jeopardy) and BuffPresent(glyph_of_double_jeopardy_buff) and Spell(judgment text=double) or Spell(judgment)
			{
				#wait,sec=cooldown.judgment.remains,if=cooldown.judgment.remains>0&cooldown.judgment.remains<=0.35
				unless SpellCooldown(judgment) > 0 and SpellCooldown(judgment) <= 0.35 and SpellCooldown(judgment) > 0
				{
					unless Enemies() > 1 and not Glyph(glyph_of_focused_shield) and Spell(avengers_shield) or Talent(sanctified_wrath_talent) and Spell(holy_wrath) or BuffPresent(grand_crusader_buff) and Spell(avengers_shield) or BuffRemaining(sacred_shield_buff) < 2 and Spell(sacred_shield) or Glyph(glyph_of_final_wrath) and target.HealthPercent() <= 20 and Spell(holy_wrath) or Spell(avengers_shield)
					{
						#lights_hammer,if=!talent.seraphim.enabled|buff.seraphim.remains>10|cooldown.seraphim.remains<6
						if not Talent(seraphim_talent) or BuffRemaining(seraphim_buff) > 10 or SpellCooldown(seraphim) < 6 Spell(lights_hammer)

						unless { not Talent(seraphim_talent) or BuffPresent(seraphim_buff) or SpellCooldown(seraphim) > 5 or TimeInCombat() < 5 } and Spell(holy_prism)
						{
							#consecration,if=target.debuff.flying.down&active_enemies>=3
							if target.True(debuff_flying_down) and Enemies() >= 2 Spell(consecration)
							#execution_sentence,if=!talent.seraphim.enabled|buff.seraphim.up|time<12
							if not Talent(seraphim_talent) or BuffPresent(seraphim_buff) or TimeInCombat() < 12 Spell(execution_sentence)

							unless Spell(hammer_of_wrath) or BuffRemaining(sacred_shield_buff) < 8 and Spell(sacred_shield)
							{
								#consecration,if=target.debuff.flying.down
								if target.True(debuff_flying_down) Spell(consecration)
							}
						}
					}
				}
			}
		}
	}
}

AddFunction ProtectionDefaultCdActions
{
	if HealthPercent() <= LayOnHandsValue()  Spell(lay_on_hands)
	#rebuke
	ProtectionInterruptActions()
	#blood_fury
	Spell(blood_fury_apsp)
	#berserking
	Spell(berserking)
	#arcane_torrent
	Spell(arcane_torrent_holy)
	#holy_avenger
	Spell(holy_avenger)
	#potion,name=draenic_armor,if=buff.shield_of_the_righteous.down&buff.seraphim.down&buff.divine_protection.down&buff.guardian_of_ancient_kings.down&buff.ardent_defender.down
	if BuffExpires(shield_of_the_righteous_buff) and BuffExpires(seraphim_buff) and BuffExpires(divine_protection_buff) and BuffExpires(guardian_of_ancient_kings_buff) and BuffExpires(ardent_defender_buff) ProtectionUsePotionArmor()
	#divine_protection,if=time<5|!talent.seraphim.enabled|(buff.seraphim.down&cooldown.seraphim.remains>5&cooldown.seraphim.remains<9)
	if HealthPercent() <= DivineProtectionValue() Spell(divine_protection)
	#guardian_of_ancient_kings,if=time<5|(buff.holy_avenger.down&buff.shield_of_the_righteous.down&buff.divine_protection.down)
	if BuffExpires(divine_protection_buff) and HealthPercent() <= GuardianValue() Spell(guardian_of_ancient_kings)
	#ardent_defender,if=time<5|(buff.holy_avenger.down&buff.shield_of_the_righteous.down&buff.divine_protection.down&buff.guardian_of_ancient_kings.down)
	if BuffExpires(divine_protection_buff) and BuffExpires(guardian_of_ancient_kings_buff) and HealthPercent() <= ArdentDefenderValue()  Spell(ardent_defender)
}

### actions.max_dps

AddFunction ProtectionMaxDpsMainActions
{
	#avengers_shield,if=buff.grand_crusader.react&active_enemies>1&!glyph.focused_shield.enabled
	if BuffPresent(grand_crusader_buff) and Enemies() > 1 and not Glyph(glyph_of_focused_shield) Spell(avengers_shield)
	#holy_wrath,if=talent.sanctified_wrath.enabled&(buff.seraphim.react|(glyph.final_wrath.enabled&target.health.pct<=20))
	if Talent(sanctified_wrath_talent) and { BuffPresent(seraphim_buff) or Glyph(glyph_of_final_wrath) and target.HealthPercent() <= 20 } Spell(holy_wrath)
	#hammer_of_the_righteous,if=active_enemies>=3
	if Enemies() >= 2 Spell(hammer_of_the_righteous)
	if Enemies() >= 2 Spell(consecration)
	#judgment,if=talent.empowered_seals.enabled&buff.liadrins_righteousness.down
	if Talent(empowered_seals_talent) and BuffExpires(liadrins_righteousness_buff) Spell(judgment)
	#crusader_strike
	Spell(crusader_strike)
	#wait,sec=cooldown.crusader_strike.remains,if=cooldown.crusader_strike.remains>0&cooldown.crusader_strike.remains<=0.35
	unless SpellCooldown(crusader_strike) > 0 and SpellCooldown(crusader_strike) <= 0.35 and SpellCooldown(crusader_strike) > 0
	{
		#judgment,cycle_targets=1,if=glyph.double_jeopardy.enabled&last_judgment_target!=target
		if Glyph(glyph_of_double_jeopardy) and BuffPresent(glyph_of_double_jeopardy_buff) Spell(judgment text=double)
		#judgment
		Spell(judgment)
		#wait,sec=cooldown.judgment.remains,if=cooldown.judgment.remains>0&cooldown.judgment.remains<=0.35
		unless SpellCooldown(judgment) > 0 and SpellCooldown(judgment) <= 0.35 and SpellCooldown(judgment) > 0
		{
			#avengers_shield,if=active_enemies>1&!glyph.focused_shield.enabled
			if Enemies() > 1 and not Glyph(glyph_of_focused_shield) Spell(avengers_shield)
			#holy_wrath,if=talent.sanctified_wrath.enabled
			if Talent(sanctified_wrath_talent) Spell(holy_wrath)
			#avengers_shield,if=buff.grand_crusader.react
			if BuffPresent(grand_crusader_buff) Spell(avengers_shield)
			#holy_wrath,if=glyph.final_wrath.enabled&target.health.pct<=20
			if Glyph(glyph_of_final_wrath) and target.HealthPercent() <= 20 Spell(holy_wrath)
			#avengers_shield
			Spell(avengers_shield)
			#seal_of_righteousness,if=talent.empowered_seals.enabled&!seal.righteousness
			if Talent(empowered_seals_talent) and not Stance(paladin_seal_of_righteousness) Spell(seal_of_righteousness)
			#holy_prism
			Spell(holy_prism)
			#hammer_of_wrath
			Spell(hammer_of_wrath)
			#holy_wrath
			Spell(holy_wrath)
			#sacred_shield
			#Spell(sacred_shield)
			#flash_of_light,if=talent.selfless_healer.enabled&buff.selfless_healer.stack>=3
			if Talent(selfless_healer_talent) and BuffStacks(selfless_healer_buff) >= 3 Spell(flash_of_light)
		}
	}
}

AddFunction ProtectionMaxDpsShortCdActions
{
	if HealthPercent() <= LayOnHandsValue()  Spell(lay_on_hands)
	#rebuke
	ProtectionInterruptActions()
	#auto_attack
	#ProtectionGetInMeleeRange()
	#speed_of_light,if=movement.remains>1
	if 0 > 1 Spell(speed_of_light)
	#seraphim
	Spell(seraphim)
	if target.InRange(rebuke)
	{
		#shield_of_the_righteous,if=buff.divine_purpose.react
		if BuffPresent(divine_purpose_buff) Spell(shield_of_the_righteous)
		#shield_of_the_righteous,if=(holy_power>=5|talent.holy_avenger.enabled)&(!talent.seraphim.enabled|cooldown.seraphim.remains>5)
		if { HolyPower() >= 5 or Talent(holy_avenger_talent) } and { not Talent(seraphim_talent) or SpellCooldown(seraphim) > 5 } Spell(shield_of_the_righteous)
		#shield_of_the_righteous,if=buff.holy_avenger.remains>time_to_hpg&(!talent.seraphim.enabled|cooldown.seraphim.remains>time_to_hpg)
		if BuffRemaining(holy_avenger_buff) > ProtectionTimeToHPG() and { not Talent(seraphim_talent) or SpellCooldown(seraphim) > ProtectionTimeToHPG() } Spell(shield_of_the_righteous)
    }
	unless BuffPresent(grand_crusader_buff) and Enemies() > 1 and not Glyph(glyph_of_focused_shield) and Spell(avengers_shield) or Talent(sanctified_wrath_talent) and { BuffPresent(seraphim_buff) or Glyph(glyph_of_final_wrath) and target.HealthPercent() <= 20 } and Spell(holy_wrath) or Enemies() >= 2 and Spell(hammer_of_the_righteous) or Talent(empowered_seals_talent) and BuffExpires(liadrins_righteousness_buff) and Spell(judgment) or Spell(crusader_strike)
	{
		#wait,sec=cooldown.crusader_strike.remains,if=cooldown.crusader_strike.remains>0&cooldown.crusader_strike.remains<=0.35
		unless SpellCooldown(crusader_strike) > 0 and SpellCooldown(crusader_strike) <= 0.35 and SpellCooldown(crusader_strike) > 0
		{
			unless Glyph(glyph_of_double_jeopardy) and BuffPresent(glyph_of_double_jeopardy_buff) and Spell(judgment text=double) or Spell(judgment)
			{
				#wait,sec=cooldown.judgment.remains,if=cooldown.judgment.remains>0&cooldown.judgment.remains<=0.35
				unless SpellCooldown(judgment) > 0 and SpellCooldown(judgment) <= 0.35 and SpellCooldown(judgment) > 0
				{
					unless Enemies() > 1 and not Glyph(glyph_of_focused_shield) and Spell(avengers_shield) or Talent(sanctified_wrath_talent) and Spell(holy_wrath) or BuffPresent(grand_crusader_buff) and Spell(avengers_shield)
					{
						#execution_sentence,if=active_enemies<3
						if Enemies() < 3 Spell(execution_sentence)

						unless Glyph(glyph_of_final_wrath) and target.HealthPercent() <= 20 and Spell(holy_wrath) or Spell(avengers_shield) or Talent(empowered_seals_talent) and not Stance(paladin_seal_of_righteousness) and Spell(seal_of_righteousness)
						{
							#lights_hammer
							Spell(lights_hammer)

							unless Spell(holy_prism)
							{
								#consecration,if=target.debuff.flying.down&active_enemies>=3
								if target.True(debuff_flying_down) and Enemies() >= 2 Spell(consecration)
								#execution_sentence
								Spell(execution_sentence)

								unless Spell(hammer_of_wrath)
								{
									#consecration,if=target.debuff.flying.down
									if target.True(debuff_flying_down) Spell(consecration)
								}
							}
						}
					}
				}
			}
		}
	}
}

AddFunction ProtectionMaxDpsCdActions
{
    if HealthPercent() <= LayOnHandsValue()  Spell(lay_on_hands)
	#blood_fury
	Spell(blood_fury_apsp)
	#berserking
	Spell(berserking)
	#arcane_torrent
	Spell(arcane_torrent_holy)
	#holy_avenger
	Spell(holy_avenger)
	#potion,name=draenic_armor,if=buff.holy_avenger.up|(!talent.holy_avenger.enabled&(buff.seraphim.up|(!talent.seraphim.enabled&buff.bloodlust.react)))|target.time_to_die<=20
	if BuffPresent(holy_avenger_buff) or not Talent(holy_avenger_talent) and { BuffPresent(seraphim_buff) or not Talent(seraphim_talent) and BuffPresent(burst_haste_buff any=1) } or target.TimeToDie() <= 20 ProtectionUsePotionArmor()
}

### actions.max_survival

AddFunction ProtectionMaxSurvivalMainActions
{
	#hammer_of_the_righteous,if=active_enemies>=3
	if Enemies() >= 2 Spell(hammer_of_the_righteous)
	if Enemies() >= 2 Spell(consecration)
	#crusader_strike
	Spell(crusader_strike)
	#wait,sec=cooldown.crusader_strike.remains,if=cooldown.crusader_strike.remains>0&cooldown.crusader_strike.remains<=0.35
	unless SpellCooldown(crusader_strike) > 0 and SpellCooldown(crusader_strike) <= 0.35 and SpellCooldown(crusader_strike) > 0
	{
		#judgment,cycle_targets=1,if=glyph.double_jeopardy.enabled&last_judgment_target!=target
		if Glyph(glyph_of_double_jeopardy) and BuffPresent(glyph_of_double_jeopardy_buff) Spell(judgment text=double)
		#judgment
		Spell(judgment)
		#wait,sec=cooldown.judgment.remains,if=cooldown.judgment.remains>0&cooldown.judgment.remains<=0.35
		unless SpellCooldown(judgment) > 0 and SpellCooldown(judgment) <= 0.35 and SpellCooldown(judgment) > 0
		{
			#avengers_shield,if=buff.grand_crusader.react&active_enemies>1
			if BuffPresent(grand_crusader_buff) and Enemies() > 1 Spell(avengers_shield)
			#holy_wrath,if=talent.sanctified_wrath.enabled
			if Talent(sanctified_wrath_talent) Spell(holy_wrath)
			#avengers_shield,if=buff.grand_crusader.react
			if BuffPresent(grand_crusader_buff) Spell(avengers_shield)
			#sacred_shield,if=target.dot.sacred_shield.remains<2
			if BuffRemaining(sacred_shield_buff) < 2 Spell(sacred_shield)
			#avengers_shield
			Spell(avengers_shield)
			#holy_prism
			Spell(holy_prism)
			#flash_of_light,if=talent.selfless_healer.enabled&buff.selfless_healer.stack>=3
			if Talent(selfless_healer_talent) and BuffStacks(selfless_healer_buff) >= 3 Spell(flash_of_light)
			#hammer_of_wrath
			Spell(hammer_of_wrath)
			#sacred_shield,if=target.dot.sacred_shield.remains<8
			if BuffRemaining(sacred_shield_buff) < 8 Spell(sacred_shield)
			#holy_wrath,if=glyph.final_wrath.enabled&target.health.pct<=20
			if Glyph(glyph_of_final_wrath) and target.HealthPercent() <= 20 Spell(holy_wrath)
			#holy_wrath
			Spell(holy_wrath)
			#sacred_shield
			#Spell(sacred_shield)
		}
	}
}

AddFunction ProtectionMaxSurvivalShortCdActions
{
	#auto_attack
	#ProtectionGetInMeleeRange()
	#speed_of_light,if=movement.remains>1
	if 0 > 1 Spell(speed_of_light)
	#seraphim,if=buff.divine_protection.down&cooldown.divine_protection.remains>0
	if BuffExpires(divine_protection_buff) and SpellCooldown(divine_protection) > 0 Spell(seraphim)
	#eternal_flame,if=buff.eternal_flame.remains<2&buff.bastion_of_glory.react>2&(holy_power>=3|buff.divine_purpose.react|buff.bastion_of_power.react)
	if BuffRemaining(eternal_flame_buff) < 2 and { HolyPower() >= 3 or BuffPresent(divine_purpose_buff) } and HealthPercent() <= WoGValue() { Spell(eternal_flame) Spell(word_of_glory) }
	#eternal_flame,if=buff.bastion_of_power.react&buff.bastion_of_glory.react>=5
	#if BuffPresent(bastion_of_power_buff) and BuffStacks(bastion_of_glory_buff) >= 1 and HealthPercent() <= WoGValue() { Spell(eternal_flame) Spell(word_of_glory) }
	if target.InRange(rebuke)
	{
		#shield_of_the_righteous,if=buff.divine_purpose.react
		if BuffPresent(divine_purpose_buff) Spell(shield_of_the_righteous)
		#shield_of_the_righteous,if=(holy_power>=5|incoming_damage_1500ms>=health.max*0.3)&(!talent.seraphim.enabled|cooldown.seraphim.remains>5)
		if { HolyPower() >= 5 or IncomingDamage(1.5) >= MaxHealth() * 0.3 } and { not Talent(seraphim_talent) or SpellCooldown(seraphim) > 5 } Spell(shield_of_the_righteous)
		#shield_of_the_righteous,if=buff.holy_avenger.remains>time_to_hpg&(!talent.seraphim.enabled|cooldown.seraphim.remains>time_to_hpg)
		if BuffRemaining(holy_avenger_buff) > ProtectionTimeToHPG() and { not Talent(seraphim_talent) or SpellCooldown(seraphim) > ProtectionTimeToHPG() } Spell(shield_of_the_righteous)
    }
	unless Enemies() >= 2 and Spell(hammer_of_the_righteous) or Spell(crusader_strike)
	{
		#wait,sec=cooldown.crusader_strike.remains,if=cooldown.crusader_strike.remains>0&cooldown.crusader_strike.remains<=0.35
		unless SpellCooldown(crusader_strike) > 0 and SpellCooldown(crusader_strike) <= 0.35 and SpellCooldown(crusader_strike) > 0
		{
			unless Glyph(glyph_of_double_jeopardy) and BuffPresent(glyph_of_double_jeopardy_buff) and Spell(judgment text=double) or Spell(judgment)
			{
				#wait,sec=cooldown.judgment.remains,if=cooldown.judgment.remains>0&cooldown.judgment.remains<=0.35
				unless SpellCooldown(judgment) > 0 and SpellCooldown(judgment) <= 0.35 and SpellCooldown(judgment) > 0
				{
					unless BuffPresent(grand_crusader_buff) and Enemies() > 1 and Spell(avengers_shield) or Talent(sanctified_wrath_talent) and Spell(holy_wrath) or BuffPresent(grand_crusader_buff) and Spell(avengers_shield) or BuffRemaining(sacred_shield_buff) < 2 and Spell(sacred_shield) or Spell(avengers_shield)
					{
						#lights_hammer
						Spell(lights_hammer)

						unless Spell(holy_prism)
						{
							#consecration,if=target.debuff.flying.down&active_enemies>=3
							if target.True(debuff_flying_down) and Enemies() >= 2 Spell(consecration)
							#execution_sentence
							Spell(execution_sentence)

							unless Talent(selfless_healer_talent) and BuffStacks(selfless_healer_buff) >= 3 and Spell(flash_of_light) or Spell(hammer_of_wrath) or BuffRemaining(sacred_shield_buff) < 8 and Spell(sacred_shield) or Glyph(glyph_of_final_wrath) and target.HealthPercent() <= 20 and Spell(holy_wrath)
							{
								#consecration,if=target.debuff.flying.down&!ticking
								if target.True(debuff_flying_down) and not target.DebuffPresent(consecration_debuff) Spell(consecration)
							}
						}
					}
				}
			}
		}
	}
}

AddFunction ProtectionMaxSurvivalCdActions
{
    if HealthPercent() <= LayOnHandsValue()  Spell(lay_on_hands)
	ProtectionInterruptActions()
	#blood_fury
	Spell(blood_fury_apsp)
	#berserking
	Spell(berserking)
	#arcane_torrent
	Spell(arcane_torrent_holy)
	#holy_avenger
	Spell(holy_avenger)
	#potion,name=draenic_armor,if=buff.shield_of_the_righteous.down&buff.seraphim.down&buff.divine_protection.down&buff.guardian_of_ancient_kings.down&buff.ardent_defender.down
	if BuffExpires(shield_of_the_righteous_buff) and BuffExpires(seraphim_buff) and BuffExpires(divine_protection_buff) and BuffExpires(guardian_of_ancient_kings_buff) and BuffExpires(ardent_defender_buff) ProtectionUsePotionArmor()
	#divine_protection,if=time<5|!talent.seraphim.enabled|(buff.seraphim.down&cooldown.seraphim.remains>5&cooldown.seraphim.remains<9)
	if HealthPercent() <= DivineProtectionValue() Spell(divine_protection)
	#guardian_of_ancient_kings,if=buff.holy_avenger.down&buff.shield_of_the_righteous.down&buff.divine_protection.down
	if BuffExpires(divine_protection_buff) and HealthPercent() <= GuardianValue() Spell(guardian_of_ancient_kings)
	#ardent_defender,if=buff.holy_avenger.down&buff.shield_of_the_righteous.down&buff.divine_protection.down&buff.guardian_of_ancient_kings.down
	if BuffExpires(divine_protection_buff) and BuffExpires(guardian_of_ancient_kings_buff) and HealthPercent() <= ArdentDefenderValue() Spell(ardent_defender)
}

### actions.precombat

AddFunction ProtectionPrecombatMainActions
{
	#flask,type=greater_draenic_stamina_flask
	#flask,type=greater_draenic_strength_flask,if=role.attack|using_apl.max_dps
	#food,type=whiptail_fillet
	#food,type=pickled_eel,if=role.attack|using_apl.max_dps
	#blessing_of_kings,if=(!aura.str_agi_int.up)&(aura.mastery.up)
	if not BuffPresent(str_agi_int_buff any=1) and BuffPresent(mastery_buff any=1) and BuffExpires(mastery_buff) and CheckBoxOn(opt_auto_blessing) and CheckBoxOn(opt_auto_blessing) and CheckBoxOn(opt_auto_blessing) Spell(blessing_of_kings)
	#blessing_of_might,if=!aura.mastery.up
	if not BuffPresent(mastery_buff any=1) and CheckBoxOn(opt_auto_blessing) and CheckBoxOn(opt_auto_blessing) and CheckBoxOn(opt_auto_blessing) Spell(blessing_of_might)
	#seal_of_insight,if=!using_apl.max_dps|using_apl.max_survival
	if not List(opt_using_apl max_dps) or List(opt_using_apl max_survival) Spell(seal_of_insight)
	#seal_of_righteousness,if=role.attack|using_apl.max_dps
	if False(role_attack) or List(opt_using_apl max_dps) Spell(seal_of_righteousness)
	#righteous_fury,if=buff.righteous_fury.down
	if BuffExpires(righteous_fury_buff) and CheckBoxOn(opt_righteous_fury_check) Spell(righteous_fury)
	#sacred_shield
	if BuffRemaining(sacred_shield_buff) < 2 Spell(sacred_shield)
}

AddFunction ProtectionPrecombatShortCdPostConditions
{
	not BuffPresent(str_agi_int_buff any=1) and BuffPresent(mastery_buff any=1) and BuffExpires(mastery_buff) and Spell(blessing_of_kings) or not BuffPresent(mastery_buff any=1) and Spell(blessing_of_might) or { not List(opt_using_apl max_dps) or List(opt_using_apl max_survival) } and Spell(seal_of_insight) or { False(role_attack) or List(opt_using_apl max_dps) } and Spell(seal_of_righteousness) or BuffExpires(righteous_fury_buff) and CheckBoxOn(opt_righteous_fury_check) and Spell(righteous_fury) or Spell(sacred_shield)
}

AddFunction ProtectionPrecombatCdActions
{
	unless not BuffPresent(str_agi_int_buff any=1) and BuffPresent(mastery_buff any=1) and BuffExpires(mastery_buff) and Spell(blessing_of_kings) or not BuffPresent(mastery_buff any=1) and Spell(blessing_of_might) or { not List(opt_using_apl max_dps) or List(opt_using_apl max_survival) } and Spell(seal_of_insight) or { False(role_attack) or List(opt_using_apl max_dps) } and Spell(seal_of_righteousness) or BuffExpires(righteous_fury_buff) and CheckBoxOn(opt_righteous_fury_check) and Spell(righteous_fury) or Spell(sacred_shield)
	{
		#snapshot_stats
		#potion,name=draenic_armor
		ProtectionUsePotionArmor()
	}
}

AddFunction ProtectionPrecombatCdPostConditions
{
	not BuffPresent(str_agi_int_buff any=1) and BuffPresent(mastery_buff any=1) and BuffExpires(mastery_buff) and Spell(blessing_of_kings) or not BuffPresent(mastery_buff any=1) and Spell(blessing_of_might) or { not List(opt_using_apl max_dps) or List(opt_using_apl max_survival) } and Spell(seal_of_insight) or { False(role_attack) or List(opt_using_apl max_dps) } and Spell(seal_of_righteousness) or BuffExpires(righteous_fury_buff) and CheckBoxOn(opt_righteous_fury_check) and Spell(righteous_fury) or Spell(sacred_shield)
}

### Protection icons.

AddCheckBox(opt_paladin_protection_aoe L(AOE) default specialization=protection)




AddIcon enemies=1 help=main specialization=protection
{
	if not InCombat() ProtectionPrecombatCdActions()
	unless not InCombat() and ProtectionPrecombatCdPostConditions()
	{
		if List(opt_using_apl normal) ProtectionDefaultCdActions()
		if List(opt_using_apl max_survival) ProtectionMaxSurvivalCdActions()
		if List(opt_using_apl max_dps) ProtectionMaxDpsCdActions()
	}
	unless not InCombat() and ProtectionPrecombatShortCdPostConditions()
	{
		if List(opt_using_apl normal) ProtectionDefaultShortCdActions()
		if List(opt_using_apl max_survival) ProtectionMaxSurvivalShortCdActions()
		if List(opt_using_apl max_dps) ProtectionMaxDpsShortCdActions()
	}
	if not InCombat() ProtectionPrecombatMainActions()
	ProtectionGetInMeleeRange()
	if List(opt_using_apl normal) ProtectionDefaultMainActions()
	if List(opt_using_apl max_survival) ProtectionMaxSurvivalMainActions()
	if List(opt_using_apl max_dps) ProtectionMaxDpsMainActions()
}

AddIcon checkbox=opt_paladin_protection_aoe help=aoe specialization=protection
{
	if not InCombat() ProtectionPrecombatCdActions()
	unless not InCombat() and ProtectionPrecombatCdPostConditions()
	{
		if List(opt_using_apl normal) ProtectionDefaultCdActions()
		if List(opt_using_apl max_survival) ProtectionMaxSurvivalCdActions()
		if List(opt_using_apl max_dps) ProtectionMaxDpsCdActions()
	}
	unless not InCombat() and ProtectionPrecombatShortCdPostConditions()
	{
		if List(opt_using_apl normal) ProtectionDefaultShortCdActions()
		if List(opt_using_apl max_survival) ProtectionMaxSurvivalShortCdActions()
		if List(opt_using_apl max_dps) ProtectionMaxDpsShortCdActions()
	}
	if not InCombat() ProtectionPrecombatMainActions()
	ProtectionGetInMeleeRange()
	if List(opt_using_apl normal) ProtectionDefaultMainActions()
	if List(opt_using_apl max_survival) ProtectionMaxSurvivalMainActions()
	if List(opt_using_apl max_dps) ProtectionMaxDpsMainActions()
}


### Required symbols
# arcane_torrent_holy
# ardent_defender
# ardent_defender_buff
# avengers_shield
# bastion_of_glory_buff
# bastion_of_power_buff
# berserking
# blessing_of_kings
# blessing_of_might
# blinding_light
# blood_fury_apsp
# consecration
# consecration_debuff
# crusader_strike
# divine_protection
# divine_protection_buff
# divine_purpose_buff
# draenic_armor_potion
# empowered_seals_talent
# eternal_flame
# eternal_flame_buff
# execution_sentence
# fist_of_justice
# flash_of_light
# glyph_of_double_jeopardy
# glyph_of_double_jeopardy_buff
# glyph_of_final_wrath
# glyph_of_focused_shield
# grand_crusader_buff
# guardian_of_ancient_kings
# guardian_of_ancient_kings_buff
# hammer_of_justice
# hammer_of_the_righteous
# hammer_of_wrath
# holy_avenger
# holy_avenger_buff
# holy_avenger_talent
# holy_prism
# holy_wrath
# judgment
# liadrins_righteousness_buff
# lights_hammer
# quaking_palm
# rebuke
# righteous_fury
# righteous_fury_buff
# sacred_shield
# sacred_shield_buff
# sanctified_wrath_talent
# seal_of_insight
# seal_of_righteousness
# selfless_healer_buff
# selfless_healer_talent
# seraphim
# seraphim_buff
# seraphim_talent
# shield_of_the_righteous
# shield_of_the_righteous_buff
# speed_of_light
# uthers_insight_buff
# war_stomp
]]
	OvaleScripts:RegisterScript("PALADIN", "protection", name, desc, code, "script")
end
